Conditions | 12 |
Paths | 3 |
Total Lines | 55 |
Lines | 55 |
Ratio | 100 % |
Tests | 32 |
CRAP Score | 12 |
Changes | 1 | ||
Bugs | 1 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like index.js ➔ decode often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | 'use strict'; |
||
90 | function decode(str) { |
||
91 | 24 | if (str.length < 8) { |
|
92 | 1 | throw new TypeError(str + ' too short'); |
|
93 | } |
||
94 | 23 | if (str.length > 90) { |
|
95 | 1 | throw new TypeError(str + ' too long'); |
|
96 | } |
||
97 | |||
98 | // don't allow mixed case |
||
99 | 22 | var lowered = str.toLowerCase(); |
|
100 | 22 | var uppered = str.toUpperCase(); |
|
101 | 22 | if (str !== lowered && str !== uppered) { |
|
102 | 1 | throw new Error('Mixed-case string ' + str); |
|
103 | } |
||
104 | |||
105 | 21 | str = lowered; |
|
106 | |||
107 | 21 | var split = str.lastIndexOf(SEPARATOR); |
|
108 | 21 | if (split === -1) { |
|
109 | 1 | throw new Error('No separator character for ' + str); |
|
110 | } |
||
111 | |||
112 | 20 | if (split === 0) { |
|
113 | 1 | throw new Error('Missing prefix for ' + str); |
|
114 | } |
||
115 | |||
116 | 19 | var prefix = str.slice(0, split); |
|
117 | 19 | var wordChars = str.slice(split + 1); |
|
118 | 19 | if (wordChars.length < 6) { |
|
119 | 1 | throw new Error('Data too short'); |
|
120 | } |
||
121 | |||
122 | 18 | var chk = prefixChk(prefix); |
|
123 | 18 | var words = []; |
|
124 | 18 | for (var i = 0; i < wordChars.length; ++i) { |
|
125 | 748 | var c = wordChars.charAt(i); |
|
126 | 748 | var v = ALPHABET_MAP[c]; |
|
127 | 748 | if (v === undefined) { |
|
128 | 1 | throw new Error('Unknown character ' + c); |
|
129 | } |
||
130 | |||
131 | 747 | chk = polymodStep(chk).xor(new BigInteger('' + v)); |
|
132 | // not in the checksum? |
||
133 | 747 | if (i + CSLEN >= wordChars.length) { |
|
134 | 136 | continue; |
|
135 | } |
||
136 | 611 | words.push(v); |
|
137 | } |
||
138 | |||
139 | 17 | if (chk.toString(10) !== '1') { |
|
140 | 9 | throw new Error('Invalid checksum for ' + str); |
|
141 | } |
||
142 | |||
143 | 8 | return { prefix: prefix, words: words }; |
|
144 | } |
||
145 | |||
186 | module.exports = { decode: decode, encode: encode, toWords: toWords, fromWords: fromWords }; |